package com.yonyou.crm.sact.actrecord.ctrl;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yonyou.crm.common.exception.CrmMessageException;
import com.yonyou.crm.common.page.entity.Page;
import com.yonyou.crm.common.rest.constant.HttpKeyConstant;
import com.yonyou.crm.common.rest.constant.PCCodeConstant;
import com.yonyou.crm.common.rest.util.RequestUtil;
import com.yonyou.crm.common.rest.util.ResponseUtil;
import com.yonyou.crm.sact.actrecord.entity.ActRecordVO;
import com.yonyou.crm.sact.actrecord.rmitf.IActRecordRmService;

@RestController
@RequestMapping(value="/sact/actrecords")
public class ActRecordCtrl {
	
	@Autowired
	private IActRecordRmService service;
	
	/**
	 * 行动记录列表
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException
	 */
	@RequestMapping(method = RequestMethod.GET)
	public Object getList(HttpServletRequest request,
			HttpServletResponse response) throws CrmMessageException {
		JSONObject paramObj = RequestUtil.getParamObject(request);
		if(paramObj == null){
			Page<ActRecordVO> actRecordPage = service.getPage(new Page<ActRecordVO>(
					0, 0), null);
			return ResponseUtil.getObject(actRecordPage);
		}else{
			@SuppressWarnings("unchecked")
			Map<String, Object> condMap = (Map<String, Object>) paramObj.get("searchMap");
			//本页面不进行分页查询，page参数在mapper中不使用
			Page<ActRecordVO> actRecordPage = service.getPage(getRequestPage(paramObj), condMap);
			return ResponseUtil.getObject(actRecordPage);
		}
	}
	
	private Page<ActRecordVO> getRequestPage(JSONObject paramObj) throws CrmMessageException {
		
		Integer page = paramObj.getInt(HttpKeyConstant.Page);
		Integer pageSize = paramObj.getInt(HttpKeyConstant.PageSize);
		
		if (page == null) {
        	throw new CrmMessageException(PCCodeConstant.ServerError, "page不能为空");
		}
		
		if (pageSize == null) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "pageSize不能为空");
		}
		
		return new Page<ActRecordVO>(page, pageSize);
	}
	
	/**
	 * 行动记录新增
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException
	 */
	@RequestMapping(method = RequestMethod.POST)
	public Object save(HttpServletRequest request,
			HttpServletResponse response) throws CrmMessageException {
		ActRecordVO actRecord = RequestUtil.getVO(request, ActRecordVO.class);
		actRecord = service.save(actRecord);
		return ResponseUtil.getObject(actRecord);
	}
	
	/**
	 * 行动记录编辑
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException
	 */
	@RequestMapping(value = "{id}", method = RequestMethod.PUT)
	public Object update(@PathVariable("id") Long id, HttpServletRequest request,
			HttpServletResponse response) throws CrmMessageException {
		ActRecordVO actRecord = RequestUtil.getVO(request, ActRecordVO.class);	
		if (actRecord == null) {
			throw new CrmMessageException("id不能为空");
		}
		actRecord.setId(id);
		actRecord = service.update(actRecord);
		return ResponseUtil.getObject(actRecord);
	}
	
	@RequestMapping(value = "batch", method = RequestMethod.DELETE)
	public Object batchDelete(HttpServletRequest request, HttpServletResponse response)
			throws CrmMessageException {
		JSONObject paramObj = RequestUtil.getParamObject(request);
		String idArray = paramObj.getString("ids");		

		if (idArray == null) {
			throw new CrmMessageException("ids参数为空");
		}
		String[] idStrs = idArray.split(",");
		@SuppressWarnings("unchecked")
		Map<String, Object> searchMap = (Map<String, Object>)paramObj.get("searchMap");
		Page<ActRecordVO> actRecordPage = service.batchDelete(idStrs, getRequestPage(paramObj), searchMap);
		return ResponseUtil.getObject(actRecordPage, PCCodeConstant.Success,"数据删除成功");
	}
		
	/**
	 * 行动记录详情
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException
	 */
	@RequestMapping(value = "{id}", method = RequestMethod.GET)
	public Object detail(@PathVariable("id") Long id,
			HttpServletRequest request, HttpServletResponse response)
			throws CrmMessageException {
		return null;
	}
}
